﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Platform.DTO;
using Taurus.Platform.XML;
using Taurus.Platform.Net;
using HtmlAgilityPack;
using System.Drawing;

namespace Taurus.Platform.BLL
{
    public class AccountBll
    {
        private IPersistence<UserAccount> _xml = new UserAccountPersistence();
        private Random _random = new Random();
       
        private UserHeadImage _headImg = new UserHeadImage();
        /// <summary>
        /// 初始化用户信息
        /// </summary>
        /// <param name="account"></param>
        public void InitAccount(UserAccount account)
        {
            HtmlDocument doc = Util.Login(account);
            account.FriendsCount = int.Parse(doc.DocumentNode.SelectSingleNode("id('maincontent')/div[2]/div/div[2]/div[4]/div[1]/span/a").InnerText);
            account.PicFullName = doc.DocumentNode.SelectSingleNode("id('maincontent')/div[2]/div/div[1]/div[1]/div[1]/div[1]/a/img").Attributes["src"].Value;
            _headImg.GetImage(account);

        }
        public Image GetAccountHeadImg(UserAccount account)
        {
            return _headImg.GetImage(account);
        }

        public bool AddAccount(UserAccount account)
        {
            if (Util.Authenticated(account))
            {
                _xml.Insert(account);
                _xml.Save();
                return true;
            }
            return false;
        }
        public bool UpdateAccount(UserAccount account, bool isVerify)
        {
            if (isVerify)
            {
                if (Util.Authenticated(account))
                {
                    _xml.Update(account);
                    _xml.Save();
                    return true;
                }
                return false;
            }
            else
            {
                _xml.Update(account);
                _xml.Save();
                return true;
            }
        }
        public bool UpdateAccount(UserAccount account)
        {
            return UpdateAccount(account, false);
        }
        /// <summary>
        /// 更新好友账号，如果好友账号是监控账号则只做关联不做更新
        /// </summary>
        /// <param name="friend"></param>
        public void UpdateFriendAccount(UserAccount friend)
        {
            UserAccount ua = _xml.GetObject(friend.UID);
            if (ua == null || ua.Password == string.Empty)
            {
                _xml.Update(friend);
                _xml.Save();
            }
        }

        public void DeleteAccount(UserAccount account)
        {
            _xml.Delete(account);
            _xml.Save();
        }

        public List<UserAccount> GetAllAccount()
        {
            return _xml.GetList(ua=>ua.Password!=string.Empty);
        }

        public List<UserAccount> GetUserAccount()
        {
            return _xml.GetList(f => !f.IsPublic && f.Password != string.Empty);
        }

        public UserAccount GetUserAccountByUID(string uid)
        {
            return _xml.GetObject(uid);
        }
        /// <summary>
        /// 随机返回一个公共账号
        /// </summary>
        /// <returns></returns>
        public UserAccount GetRandomPublicAccount()
        {
            List<UserAccount> list = _xml.GetList(f => f.IsPublic);
            return list[_random.Next(list.Count)];
        }

       
    }
}
